import base64
import datetime
import hashlib
import json
import re

from re_common.baselibrary.utils.baseencode import BaseEncode
from re_common.baselibrary.utils.baseurl import BaseUrl

from apps.crawler_platform.core_platform.core_g import CoreSqlValue
from apps.crawler_platform.core_platform.g_model import CallBackModel, OtherHomeModel, DealModel, DealInsertModel, \
    OtherArticleModel, EtlDealModel, EtlCallBackModel, OtherArticleEtlModel, T1, DealUpdateModel, OperatorSqlModel

__all__ = ["cnkiimage_cnkiimagehome_callback",
           "cnkiimage_cnkiimagearticle_callback",
           "cnkiimage_cnkiimagearticle_etl_callback"
           ]


def cnkiimage_cnkiimagearticle_callback(callmodel: CallBackModel[OtherArticleModel]) -> DealModel:
    result = DealModel()
    # print(callmodel)
    # task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    sql_model = callmodel.sql_model
    # info_dicts = {"task_name": task_info.task_name,
    #               "task_tag": task_info.task_tag,
    #               "task_tag_next": task_info.task_tag_next,
    #               "sub_db_id": sql_model.sub_db_id
    #               }
    article_json_dicts = json.loads(sql_model.article_json)
    if "1_1" in callmodel.para_dicts.keys():
        data_dicts = callmodel.para_dicts["1_1"]["data"]["1_1"]
        image_url = data_dicts["image_url"]
        image_url_id = image_url.split("/")[-1]
        article_json_dicts["image_url"] = image_url
        article_json_dicts["image_url_id"] = image_url_id

    if "1_3" in callmodel.para_dicts.keys():
        from app_main import request_ctx_var
        deal = True
        if "1_3" in request_ctx_var.get().state.pm.down_status.keys():
            if str(request_ctx_var.get().state.pm.down_status["1_3"][1]).find("1102") > -1:
                article_json_dicts["article_id"] = ""
                deal = False
        if deal:
            data_dicts_1_3 = callmodel.para_dicts["1_3"]["data"]["1_3"]
            title_url = data_dicts_1_3["title_url"]
            init_info = data_dicts_1_3["init_info"]
            try:
                article_id = BaseUrl.urlQuery2Dict(title_url)["filename"]
                article_json_dicts["article_id"] = article_id
            except:
                try:
                    article_id = BaseUrl.urlQuery2Dict(title_url)["RECID"]
                    article_json_dicts["recid"] = article_id
                    article_json_dicts["article_id"] = article_id #   article_json_dicts["article_id"] = ''
                    # 之前  article_json_dicts["article_id"] = ‘’ 是认为 有 recid给 recid ，现在发现需要用到recid
                    # 20230303
                except:
                    # 20230303
                    rule1 = re.compile(r"InitImage\('False','(.*?)','False'\);")
                    article_id = rule1.findall(init_info)[0]
                    article_json_dicts["article_id"] = article_id


    if "1_4" in callmodel.para_dicts.keys():
        data_dicts_1_4_data = callmodel.para_dicts["1_4"]["data"]
        data_dicts_1_4 = data_dicts_1_4_data.get("1")
        if data_dicts_1_4 is not None:
            result.code_dicts.update({"1_4": {"max_page": data_dicts_1_4["pages"]}})
        else:
            result.code_dicts.update({"1_4": {"max_page": 1}})
    article_json = json.dumps(article_json_dicts, ensure_ascii=False)
    result.befor_dicts.update.update({'article_json': article_json})
    return result


def cnkiimage_cnkiimagehome_callback(callmodel: CallBackModel[OtherHomeModel]) -> DealModel:
    result = DealModel()
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    sql_model = callmodel.sql_model
    info_dicts = {"task_name": task_info.task_name,
                  "task_tag": task_info.task_tag,
                  "task_tag_next": task_info.task_tag_next,
                  "sub_db_id": sql_model.sub_db_id
                  }

    data_dicts = callmodel.para_dicts["data"]["1_1"]
    max_pages = data_dicts["pages"]
    result.code_dicts = {
        "1_1": {"max_page": int(max_pages)}
    }
    if sql_model.page_index == 1:
        d_i_model = DealInsertModel()
        d_i_model.insert_pre = CoreSqlValue.insert_ig_it
        lists_update = []
        for i in range(1, int(max_pages) + 1):
            temp = info_dicts.copy()
            temp["page"] = max_pages
            temp["list_rawid"] = "None"
            temp["page_index"] = i
            temp["home_json"] = "{}"
            temp["is_active"] = 1
            del temp["task_tag_next"]
            d_i_model.lists.append(temp)
            d_u_model = DealUpdateModel()
            d_u_model.update = {'is_active': 1, "page": max_pages}
            tmp_list = [
                {
                    "key": "task_name",
                    "value": info_dicts["task_name"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "task_tag",
                    "value": info_dicts["task_tag"],
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "list_rawid",
                    "value": sql_model.list_rawid,
                    "operator": "=",
                    "description": "and"
                },
                {
                    "key": "page_index",
                    "value": i,
                    "operator": "=",
                    "description": "and"
                },
            ]
            list_op = []
            for item in tmp_list:
                op = OperatorSqlModel()
                list_op.append(op.parse_obj(item))

            d_u_model.where = list_op
            lists_update.append(d_u_model)

        result.befor_dicts.insert.append(d_i_model)
        result.befor_dicts.update_list = lists_update

    result.befor_dicts.update.update({'is_active': 1, "page": max_pages})
    insert = DealInsertModel()
    insert.insert_pre = CoreSqlValue.insert_ig_it
    for item in data_dicts["data"]:
        temp = info_dicts.copy()
        temp["task_tag"] = temp["task_tag_next"]
        del temp["task_tag_next"]
        temp["article_json"] = json.dumps(item, ensure_ascii=False)
        temp["rawid"] = str(item["image_id"])
        temp["count"] = str(len(data_dicts["data"]))
        temp["other_dicts"] = json.dumps({"page": sql_model.page_index}, ensure_ascii=False)
        insert.lists.append(temp)

    result.next_dicts.insert.append(insert)
    return result


def cnkiimage_cnkiimagearticle_etl_callback(callmodel: EtlCallBackModel[OtherArticleEtlModel, T1]) -> EtlDealModel:
    result = EtlDealModel()
    para_dicts = callmodel.para_dicts
    del para_dicts["journal_rawids"]

    def deal(value):
        result = ""
        if isinstance(value, list):
            for val in value:
                val = val.replace("\r", "").replace("\n", "").strip()
                if val != "":
                    result = result + val
        else:
            result = value
        return result

    latest_data = {}
    if "1_1" in para_dicts.keys():
        result_1_1 = para_dicts["1_1"]["data"]["1_1"]
        image_title = deal(result_1_1["title"])  # 图片名 或者说图片标题
        source = deal(result_1_1["source_text"])  # 图片来源
        image_source = source.replace(">>查看本文图片摘要", "").replace(" ", "")
        image_source_url = "https://image.cnki.net" + deal(result_1_1["source_href"]).replace("..",
                                                                                              "")  # 该url 连接到一个页面 里面记录了该图片所属文章的信息
        image_keyword = ";".join(result_1_1["image_keyword"])  # 图片关键字
        image_subject = ";".join(result_1_1["image_subject"])  # 所属学科
        image_context = deal(result_1_1["image_context"])  # 图片上下文
        latest_data["image_title"] = image_title
        latest_data["image_source"] = image_source
        latest_data["image_source_url"] = image_source_url
        latest_data["image_keyword"] = image_keyword
        latest_data["image_subject"] = image_subject
        latest_data["image_context"] = image_context
    else:
        result.status = "FAILED"
        result.code = 7
        result.err_msg = "规则解析错误 cnkiimage 1_1 不存在"
        return result

    if "1_2" in para_dicts.keys():
        img_str = para_dicts["1_2"]["data"]["1_2"]["html"]
        new_img_str = json.loads(img_str)
        img_str = new_img_str["image_base64"]
        bt = img_str.encode("utf-8")
        img_data = base64.b64decode(
            bt)

        article_dicts = json.loads(callmodel.sql_model.article_json)
        file_name = article_dicts["image_url"].split("/")[-1]
        from apps.crawler_platform.util.mmh3encode import normFileName_image, generateHashName
        file_name = normFileName_image(file_name)
        file_path = generateHashName(file_name)
        minio_path = "pic/cnkiimage/" + file_path + "/" + file_name
        # minio_bk = "crawl.dc.cqvip.com"
        result.other_info = {"image": {"path": minio_path, "image_data": img_data}}
        latest_data["image_path"] = minio_path
        latest_data["image_size"] = len(img_data)
        latest_data["image_data_base64"] = img_str
        latest_data["image_hash_md5"] = hashlib.md5(img_data).hexdigest()

    else:
        result.status = "FAILED"
        result.code = 7
        result.err_msg = "规则解析错误 cnkiimage 1_2 不存在"
        return result

    if "1_3" in para_dicts.keys():
        result_1_3 = para_dicts["1_3"]["data"]["1_3"]
        # print(result_1_3)
        article_title = deal(result_1_3["article_text"])  # 文章标题
        article_url = deal(result_1_3["article_url"])  # 文章url
        recid_url = deal(result_1_3["recid_url"])  # 文章url
        article_rawid = ""
        recid_rawid = ""
        if article_url != "":
            dicts = BaseUrl.urlQuery2Dict(article_url)
            if "filename" in dicts.keys():
                article_rawid = dicts["filename"]  # rawid
        if recid_url != "":
            dicts = BaseUrl.urlQuery2Dict(recid_url)
            if "RECID" in dicts.keys():
                recid_rawid = dicts["RECID"]  # rawid

        article_source = deal(result_1_3["article_source"])  # 文章来源
        article_source = article_source.replace("\xa0\xa0", "")
        article_keyword = ";".join(result_1_3["article_keyword"])  # 文章关键词
        article_abs = deal(result_1_3["article_abs"])  # 文章摘要

        latest_data["article_title"] = article_title
        latest_data["article_rawid"] = article_rawid
        latest_data["recid_rawid"] = recid_rawid
        latest_data["article_source"] = article_source
        latest_data["article_keyword"] = article_keyword
        latest_data["article_abs"] = article_abs
    else:
        result.status = "FAILED"
        result.code = 7
        result.err_msg = "规则解析错误 cnkiimage 1_3 不存在"
        return result

    if "1_4" in para_dicts.keys():
        result_dicts = para_dicts["1_4"]["data"]
        list_imageid = []
        list_image_info = []
        for k, v_all in result_dicts.items():
            for v in v_all["data"]:
                list_imageid.append((v["image_id"], v["title"]))
                list_image_info.append(v)
        latest_data["list_imageid"] = list_imageid
        latest_data["list_image_info"] = list_image_info

    else:
        result.status = "FAILED"
        result.code = 7
        result.err_msg = "规则解析错误 cnkiimage 1_3 不存在"
        return result
    latest_data["_id"] = callmodel.sql_model.rawid
    latest_data["update_time"] = str(datetime.datetime.now())
    result.save_data = [{"table": "cnkiimage_latest_hdd", "data": latest_data}]

    return result
